package com.waqu.common.utils.waqu;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.waqu.common.utils.text.StringTools;

/**
 * @Title WaquPicCondense.java
 * @Package com.waqu.common.utils.waqu
 * @author tangjunfeng
 * @date 2016年11月7日 下午2:58:09
 * @version V1.0
 */
public class WaquPicCondense {

	private static final Logger logger = LoggerFactory.getLogger(WaquPicCondense.class);
	private final static String ALIPAY_AVATAR_FORMAT = "@!avatar";
	private final static String QINIU_AVATAR_FORMAT = "@avatar";
	
	/** 将当前图片地址转换成蛙趣的缩略图地址 */
	public static String getPicAddress(String picAddress) {
		if (StringTools.isEmpty(picAddress))
			return picAddress;
		if (picAddress.startsWith("http://img.feixun.tv/") && picAddress.indexOf("@!") < 0) {
			return picAddress + ALIPAY_AVATAR_FORMAT;
		} else if (picAddress.indexOf("@") < 0) {
			return picAddress + QINIU_AVATAR_FORMAT;
		}
		return picAddress;
	}
	
	/** 将当前JavaBean中的图片地址转换成蛙趣的缩略图地址<br/>
	 * <b>注意：当前属性的Set方法不能存在返回值，否则将会无法修改该值</b> */
	public static <T extends Object> T updatePicAddress(T t, String picName) {
		try {
			String property = getPicAddress(BeanUtils.getProperty(t, picName));
			BeanUtils.setProperty(t, picName, property);
			return t;
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			logger.error(e.getMessage(), e);
			return t;
		}
	}
	
}